//
//  LXLMeCollectionView.m
//  真是不得姐啊
//
//  Created by 朱建鹏 on 16/8/25.
//  Copyright © 2016年 朱建鹏. All rights reserved.
//

#import "LXLMeCollectionView.h"
#import "LXLMeCollectionViewModel.h"
#import "LXLMeCollectionViewCell.h"

@interface LXLMeCollectionView () <UICollectionViewDelegate,UICollectionViewDataSource>

@property (strong,nonatomic) NSArray *CollectionArr;

@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *flowLayout;



@end

@implementation LXLMeCollectionView

-(void)awakeFromNib{

    self.delegate = self;
    
    self.dataSource = self;
    
    self.pagingEnabled = YES;
    
    self.flowLayout.minimumLineSpacing = 15;
    self.flowLayout.minimumInteritemSpacing = 5;
    
    self.flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 5, 10);
    
    
    self.bounces = NO;
    self.backgroundColor = [UIColor whiteColor];

    [self registerNib:[UINib nibWithNibName:@"LXLMeCollectionViewCell" bundle:nil]forCellWithReuseIdentifier:@"LXLMeCollectionViewCell"];
    
}
-(void)layoutSubviews{
    [super layoutSubviews];
    
    CGFloat itemW = (self.bounds.size.width - 10 * 2 - 20 * 4) / 5;
    
    CGFloat itemH = itemW + 20;
    
    self.flowLayout.itemSize = CGSizeMake(itemW, itemH);

}



-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.CollectionArr.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    LXLMeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LXLMeCollectionViewCell" forIndexPath:indexPath];
    
    LXLMeCollectionViewModel *model = self.CollectionArr[indexPath.item];
    
    cell.model = model;
    
    
    
    
    
    return cell;

}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    LXLMeCollectionViewCell *cell = (LXLMeCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    
    if([cell.model.name isEqualToString:@"更多"]){
    
        NSLog(@"%@",indexPath);
    }

}






- (void)setUrl:(NSURL *)url{
    
    [LXLMeCollectionViewModel LXLMeCollectionViewModelDataWithURL:url andKey:@"square_list" succeedBlock:^(NSArray *arr) {
        
        self.CollectionArr = arr;

        [self reloadData];
        
    } failureBlock:^(NSError *error) {
        NSLog(@"self.CollectionArr  下载错误");
        
    }];

}

@end
